﻿using System;
using System.ComponentModel.Composition;
using System.Net;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Deployment;

namespace CodeCounsel.SharePoint.DevTools.DeploymentSteps
{
    [Export(typeof(IDeploymentStep))]
    [DeploymentStep(DeploymentStepIds.WarmUpSite)]
    public class WarmUpSite
        : IDeploymentStep
    {
        public void Initialize(IDeploymentStepInfo stepInfo)
        {
            stepInfo.Name = "Warm up SharePoint Site";
            stepInfo.StatusBarMessage = "Warming up site...";
            stepInfo.Description = "Warms up the SharePoint site.";
        }

        public bool CanExecute(IDeploymentContext context)
        {
            return true;
        }

        public void Execute(IDeploymentContext context)
        {
            Uri url = context.Project.SiteUrl;
            WebRequest request = HttpWebRequest.Create(url);
            request.BeginGetResponse(
                a => request.EndGetResponse(a), null);
        }
    }
}
